package com.resou.reader.reader.animation;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Toast;

import com.resou.reader.reader.manager.BookStatus;
import com.resou.reader.reader.view.PageView;

/**
 * Created by ${Garrett} on 2018/9/28.
 * Contact me krouky@outlook.com
 */
public class NoAnimation extends BaseAnimation {

    private float downX;

    /**
     * 当前页
     */
    private Bitmap mCurrPageBitmap;
    private Canvas mCurrPageCanvas;

    private PageView view;

    @Override
    public void onMeasure(int width, int height) {
        try {
            onDestory();
            mCurrPageBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
            mCurrPageCanvas = new Canvas(mCurrPageBitmap);
        } catch (OutOfMemoryError error) {
        }
    }

    @Override
    public void onDraw(Canvas canvas) {
        drawCurrPage(canvas);
    }

    private void drawCurrPage(Canvas canvas) {
        canvas.save();
        canvas.drawBitmap(mCurrPageBitmap, 0, 0, null);
        canvas.restore();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = x;
                return true;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
            default:
                if (downX >= view.getWidth() / 2) {//下一页
                    int state = pageFactory.nextPage();
                    /**
                     * 广告页显示的时候让其刷新
                     */
                    if (pageFactory.isNexPage() && pageFactory.isAdPrePage()) {
                        pageFactory.refreshAd();
                        Log.i("广告", "onTouchEvent: 刷新");
                    }
                    if (state == BookStatus.NO_NEXT_PAGE || state == BookStatus.NO_NEXT_CHAPTER) {
//                        Toast.makeText(view.getContext(), "没有下一页啦", Toast.LENGTH_SHORT).show();
                        pageFactory.startEndBookPage();
                        return false;
                    } else if (state == BookStatus.LOAD_SUCCESS || state == 2) {
                        pageFactory.onDraw(mCurrPageCanvas);
                        view.postInvalidate();
                        return true;
                    } else {
                        return false;
                    }
                } else {// 上一页
                    int state = pageFactory.prePage();
                    if (state == BookStatus.NO_PRE_PAGE) {
                        Toast.makeText(view.getContext(), "没有上一页啦", Toast.LENGTH_SHORT).show();
                        return false;
                    } else if (state == BookStatus.LOAD_SUCCESS || state == 2) {
                        pageFactory.onDraw(mCurrPageCanvas);
                        view.postInvalidate();
                        return true;
                    } else {
                        return false;
                    }
                }
        }
        return false;
    }

    @Override
    public void bindView(PageView pageView) {
        this.view = pageView;
        init();
    }

    private void init() {
    }

    @Override
    public void computeScroll() {

    }

    @Override
    public void onDestory() {
        if (mCurrPageBitmap != null && !mCurrPageBitmap.isRecycled()) {
            mCurrPageCanvas = null;
            mCurrPageBitmap.recycle();
            mCurrPageBitmap = null;
        }
    }

    @Override
    public void refreshCurrPage() {
        pageFactory.onDraw(mCurrPageCanvas);
    }

    @Override
    public void refreshNextPage() {
    }

    @Override
    public void nextPage() {
        int status = pageFactory.nextPage();
        if (status == BookStatus.NO_NEXT_PAGE || status == BookStatus.NO_NEXT_CHAPTER) {
            Toast.makeText(view.getContext(), "没有下一页啦", Toast.LENGTH_SHORT).show();
            return;
        } else if (status == BookStatus.LOAD_SUCCESS) {
//            if (isPrepared) {
            pageFactory.onDraw(mCurrPageCanvas);
//            pageFactory.onDraw(mNextPageCanvas);
            view.postInvalidate();
//            }
        } else {
            return;
        }
    }

    @Override
    public void prePage() {
        int status = pageFactory.prePage();
        if (status == BookStatus.NO_PRE_PAGE) {
            Toast.makeText(view.getContext(), "", Toast.LENGTH_SHORT).show();
            return;
        } else if (status == BookStatus.LOAD_SUCCESS) {
//            if (isPrepared) {
            pageFactory.onDraw(mCurrPageCanvas);
//            pageFactory.onDraw(mNextPageCanvas);
            view.postInvalidate();
//        }
        } else {
            return;
        }
    }
}
